@include('Admin.public.head')
<body>
@include('Admin.public.menu')
<div class=" admin-content">
    <div class="daohang">

    </div>
    <div class="admin-biaogelist" id="appUser">
        <div class="listbiaoti am-cf">
            <ul class="am-icon-flag on"> 添加公告</ul>
            {{--<dl class="am-icon-home" style="float: right;"> 当前位置： 首页 > <a href="#">商品列表</a></dl>--}}
        </div>
        <div class="fbneirong" >
            <form class="am-form">
                {{ csrf_field() }}
                <div class="am-form-group am-cf">
                    <div class="zuo">公告标题：</div>
                    <div class="you">
                        <input type="text" class="am-form-field" id="doc-ipt-email-1" v-model="notice.notice_name" @blur="check()" placeholder="请输入标题" required>
                    </div>
                </div>
                <div class="am-form-group am-cf">
                    <div class="zuo">公告时间：</div>
                    <div class="you">
                        <el-date-picker
                                v-model="notice.time"
                                type="daterange"
                                value-format="yyyy-MM-dd"
                                range-separator="~"
                                :start-placeholder="time.start"
                                :end-placeholder="time.end">
                        </el-date-picker>
                    </div>
                </div>
                <div class="am-form-group am-cf">
                    <div class="zuo">公告内容：</div>
                    <div class="you">
                        <textarea id="editor" v-model="notice.content"></textarea>
                    </div>
                </div>
                <div class="am-form-group am-cf">
                    <div class="you" style="margin-left: 11%;">
                        <button type="button" class="am-btn am-btn-success am-radius" v-on:click="ensure()">确 定</button>&nbsp;&nbsp;
                    </div>
                </div>
            </form>
        </div>
        <script type="text/javascript" src="{{ URL::asset('js/ueditor/ueditor.config.js') }}"></script>
        <script type="text/javascript" src="{{ URL::asset('js/ueditor/ueditor.all.min.js') }}"></script>
        <script>
            function getContent(){
                var html = UE.getEditor('editor').getContent();
                return html;
            }
            window.onload = getContent;
        </script>
        <script>
            var app = new Vue({
                el:"#appUser",
                data: {
                    time:{
                        start:'开始日期',
                        end:'结束日期'
                    },
                    notice:{
                        time:'',
                        notice_name:'',
                        content:''
                    }
                },
                //事件
                methods: {
                    //提交
                    ensure:function(){
                        this.notice.content=UE.getEditor('editor').getContent();

                        var id = this.obtain('id');
                        var type = this.obtain('type')
                        if(type==='edit') {
                            if(!this.notice.time) {
                                this.notice.time = [this.time.start,this.time.end]
                            }
                            for(var item in this.notice) {
                                if(!this.notice[item]) {
                                    layer.msg('请填写正确信息')
                                    return false
                                }
                            }
                            $.post("/admin/system/setNotice", {'_token': '{{csrf_token()}}', class: 'edit',notice:{notice_name:this.notice.notice_name,content:this.notice.content},time:this.notice.time,id:id}, function (res) {
                                if (res.code) {
                                    location.href="/admin/system/notice"
                                } else {
                                    layer.msg(res.data);
                                }
                            });
                        }else {
                            for(var item in this.notice) {
                                if(!this.notice[item]) {
                                    layer.msg('请填写正确信息')
                                    return false
                                }
                            }
                            $.post("/admin/system/setNotice", {'_token': '{{csrf_token()}}', class: 'add',notice:this.notice,time:this.notice.time}, function (res) {
                                if (res.code) {
                                    location.href="/admin/system/notice"
                                } else {
                                    layer.msg(res.data);
                                }
                            });
                        }


                    },
                    obtain: function (val) {
                        var query = window.location.search.substring(1);
                        var vars = query.split("&");
                        for (var i = 0; i < vars.length; i++) {
                            var pair = vars[i].split("=");
                            if (pair[0] == val) {
                                return pair[1];
                            }
                        }
                    },
                    //初始化
                    initialize:function () {
                        var _this = this
                        var id = this.obtain('id');
                        var type = this.obtain('type')
                        if(type==='edit'){
                            $.get("/admin/system/setNotice",{class:'find',id:id},function (res) {
                                if(res.code){
                                    UE.getEditor('editor').ready(function () {
                                        //this是当前创建的编辑器实例
                                        this.setContent(res.data.content)
                                    });
                                    _this.time.start = res.data.release_time.substring(0,10)
                                    _this.time.end = res.data.deadline.substring(0,10)
                                    _this.notice = res.data
                                } else {
                                    layer.msg(res.data);
                                }
                            });
                        }
                        //编辑

                    },
                },
                //自动执行
                mounted: function () {
                    this.initialize();//分公司
                },
            })
        </script>
@include('Admin.public.foot')